package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserAvocation;
import com.lantosec.hmrp.modules.archives.mapper.UserAvocationMapper;
import com.lantosec.hmrp.modules.archives.service.IUserAvocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserAvocationCompanyVO;
import com.lantosec.hmrp.modules.archives.vo.UserAvocationParttimeVO;
import com.lantosec.hmrp.modules.archives.vo.UserAvocationVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 领导干部从事或参与营利性活动及兼职情况报告表。 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-11
 */
@Service
@Primary
@Log4j2
public class UserAvocationServiceImpl extends ServiceImpl<UserAvocationMapper, UserAvocation> implements IUserAvocationService {

    @Override
    @Transactional
    public void saveUserAvocation(UserAvocationVO vo) {
        UserAvocation userAvocation = vo.convertToEntity();
        userAvocation.setUserAvocationId(UUIDUtils.get());
        //获取当前用户id和名称
        userAvocation.setCreateId(TokenUtil.getUserId());
        userAvocation.setCreateUser(TokenUtil.getName());
        userAvocation.setCreateTime(DateUtils.nowDate());
        userAvocation.setVerifyStatus(1);
        this.save(userAvocation);
        insertUserAvocationOther(vo,userAvocation.getUserAvocationId());

    }

    /**
     * 新增个人经办企业信息和事业单位、社会团体兼职情况
     * @param vo
     */
    private void insertUserAvocationOther(UserAvocationVO vo,String userAvocationId) {
        //新增个人经办企业信息
        List<UserAvocationCompanyVO> userAvocationCompanyVO = vo.getUserAvocationCompanyVO();
        userAvocationCompanyVO.forEach(param->{
            param.setUserAvocationCompanyId(UUIDUtils.get());
            param.setUserAvocationId(userAvocationId);
            baseMapper.saveUserAvocationCompany(param);
        });
        //新增事业单位、社会团体兼职情况
        List<UserAvocationParttimeVO> userAvocationParttimeVO = vo.getUserAvocationParttimeVO();
        userAvocationParttimeVO.forEach(param->{
            param.setUserAvocationParttimeId(UUIDUtils.get());
            param.setUserAvocationId(userAvocationId);
            baseMapper.saveUserAvocationParttime(param);
        });
    }

    @Override
    @Transactional
    public void updateUserAvocation(UserAvocationVO vo) {
        String userAvocationId = vo.getUserAvocationId();
        //新增一条数据
        //saveUserAvocation(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userAvocationId)) {
            UserAvocation userAvocation = vo.convertToEntity();
            userAvocation.setVerifyStatus(1);
            //新增信息先删除
            baseMapper.deleteUserAvocationCompany(userAvocationId);
            baseMapper.deleteUserAvocationParttime(userAvocationId);
            insertUserAvocationOther(vo,userAvocationId);
            this.updateById(userAvocation);
        }else {
            saveUserAvocation(vo);
        }
    }

    @Override
    public List<UserAvocationVO> getUserAvocation(String userId) {
        List<UserAvocationVO> userAvocationVOList = new ArrayList<>();
        List<UserAvocation> userAvocationList = this.list(new QueryWrapper<UserAvocation>().eq(UserAvocation.ColName.user_id.toString(),userId).orderByDesc(UserAvocation.ColName.create_time.toString()));
        //组装个人经办企业信息和事业单位、社会团体兼职情况
        userAvocationList.forEach(param->{
            UserAvocationVO userAvocationVO = param.convertToVO();
            String userAvocationId = param.getUserAvocationId();
            //查询个人经办企业信息
            List<UserAvocationCompanyVO> userAvocationCompanyVO = baseMapper.getUserAvocationCompany(userAvocationId);
            userAvocationVO.setUserAvocationCompanyVO(userAvocationCompanyVO);
            //查询事业单位、社会团体兼职情况
            List<UserAvocationParttimeVO> userAvocationParttimeVO = baseMapper.getUserAvocationParttime(userAvocationId);
            userAvocationVO.setUserAvocationParttimeVO(userAvocationParttimeVO);
            userAvocationVOList.add(userAvocationVO);
        });
        return userAvocationVOList;
    }

    @Override
    public List<UserAvocation> getUserAvocationVerify(String userId) {
        List<UserAvocation> userAvocationList = this.list(new QueryWrapper<UserAvocation>().eq(UserAvocation.ColName.user_id.toString(),userId).in(UserAvocation.ColName.verify_status.toString(),0,2));
        return userAvocationList;
    }

    @Override
    public void verifyUserAvocation(UserAvocationVO vo) {
        UserAvocation userAvocation = vo.convertToEntity();
        Integer verifyStatus = userAvocation.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userAvocation.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userAvocation.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userAvocation.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userAvocation.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userAvocation.setVerifyId(TokenUtil.getUserId());
        userAvocation.setVerifyUser(TokenUtil.getName());
        userAvocation.setVerifyTime(DateUtils.nowDate());
        this.updateById(userAvocation);
    }

    @Override
    public List<UserAvocation> getUserAvocationHistory(String userId) {
        List<UserAvocation> userAvocationList = this.list(new QueryWrapper<UserAvocation>().eq(UserAvocation.ColName.user_id.toString(),userId).eq(UserAvocation.ColName.verify_status.toString(),4));
        return userAvocationList;
    }
}
